<?php
defined('SYSPATH') or die('No direct script access.');

class Model_Patient extends Kohana_Model
 {    
     public function searchPatientByKeyword($keyword){

         $sql = "SELECT * FROM patient_profile WHERE p_fname LIKE '%".$keyword."%' or p_lname LIKE '%".$keyword."%'";
         return Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
     }

     public function addNewPatient($data){
         return $query = DB::insert('patient_profile',
                 array('p_fname','p_lname','p_sex',"p_bday",'p_address','p_phone'))
        ->values(array($data['p_fname'],$data['p_lname'],$data['p_sex'],date($data['p_bday']),$data['p_address'],$data['p_phone']))->execute();
     }

     public function loadPatientPlace(){
         $sql = "SELECT * FROM patient_place";
         return Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
     }

     public function getPatientProfileFromPID($p_id){
         $sql = "SELECT * FROM patient_profile WHERE p_id=".$p_id;
         return Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();    
     }

     public function updateProfile($data){
         return DB::update('patient_profile')
                ->set(array(
                'p_fname'=>$data['p_fname'],
                'p_lname'=>$data['p_lname'],
                'p_sex'=>$data['p_sex'],
                'p_bday'=>$data['p_bday'],
                'p_address'=>$data['p_address'],
                'p_phone'=>$data['p_phone']
                ))
                ->where('p_id','=', $data['p_id'])
                ->execute();
     }

     public function delPatient($p_id){
         return DB::delete("patient_profile")
                    ->where('p_id', '=',$p_id)
                    ->execute();
     }

     public function getAllPatient(){
         $sql = "SELECT * FROM patient_profile";
         return Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
     }
 }
